package com.kcsj.controller.basic;
//姜瑞临
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.kcsj.domain.Inventory;
import com.kcsj.domain.ProductModel;
import com.kcsj.domain.Warehouse;
import com.kcsj.service.InventoryService;
import com.kcsj.util.Condition;
import com.kcsj.util.ControllerHelper;
import com.kcsj.util.JSONUtil;
import com.kcsj.util.MessageHelper;
import lombok.extern.slf4j.Slf4j;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

@Slf4j
@WebServlet("/inventory.ctl")
public class InventoryController extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //从JSONObject对象中读取键“id”的值（Java Object对象），req_jsonObj可能是{"id":1}
        String warehouseNo = request.getParameter("warehouseNo");
        String condition = request.getParameter("condition");
        List<Condition> conditionList = null;
        Collection<Inventory> inventories = null;
        if (condition!=null){
            conditionList = ControllerHelper.getConditions(JSONObject.parseObject(condition));
        }
        try {
            if (warehouseNo==null){
                inventories = InventoryService.getInstance().findAll(conditionList);
            }else {
                inventories = InventoryService.getInstance().find(warehouseNo);
            }
            String string_inventory =  JSON.toJSONString(inventories);
            JSONObject message = MessageHelper.addData(string_inventory);
            response.getWriter().println(message);
        } catch (SQLException e) {
            JSONObject jsonObject = MessageHelper.dateBaseError();
            log.error(e.getMessage());
            //响应respMessage_jsonObj到前端
            response.getWriter().println(jsonObject);
        } catch (Exception e) {
            e.printStackTrace();
            JSONObject netWordError = MessageHelper.netWordError();
            //响应respMessage_jsonObj到前端
            response.getWriter().println(netWordError);
            //打印异常栈，方便调试（要用log4j代替）
            log.error(e.getMessage());
        }
    }
    @Override
    protected void doPut(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //将request body转换为JSON字串
        String req_json = JSONUtil.getJSON(request);
        try { ;
            JSONObject object = JSON.parseObject(req_json);
            int changeNumber = object.getInteger("amount");
            ProductModel productModel = object.getObject("productModel",ProductModel.class);
            Warehouse warehouse = object.getObject("warehouse",Warehouse.class);
            productModel.setWarehouse(warehouse);
            System.out.println(productModel);
            String type = object.getString("type");
            InventoryService.getInstance().update(changeNumber,productModel,type);
            JSONObject message = MessageHelper.addMessage("盘点完成");
            response.getWriter().println(message);
        }catch (SQLException e) {
            e.printStackTrace();
            JSONObject message = MessageHelper.dateBaseError();
            response.getWriter().println(message);
        }catch (Exception e){
            e.printStackTrace();
            JSONObject message = MessageHelper.netWordError();
            response.getWriter().println(message);
        }
    }
}
